package com.manage.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.modules.domain.TBaseFaultPhoto;
import com.manage.modules.mapper.TBaseFaultPhotoMapper;
import com.manage.modules.service.ITBaseFaultPhotoService;
import com.manage.modules.utils.UploadUtil;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 故障信息照片Service业务层处理
 *
 * @author xu
 * 2021/8/19 10:14
 */
@Service
public class TBaseFaultPhotoServiceImpl extends ServiceImpl<TBaseFaultPhotoMapper, TBaseFaultPhoto> implements ITBaseFaultPhotoService {

    @Override
    public void delete(List<TBaseFaultPhoto> deletePhotoList) {
        if (deletePhotoList != null && deletePhotoList.size() > 0) {
            for (TBaseFaultPhoto faultPhoto : deletePhotoList) {
                // 删除照片
                UploadUtil.delete(faultPhoto.getLocal());
                // 删除对象
                boolean b = removeById(faultPhoto.getFaultPhotoId());
                System.out.println("删除成功:" + b);
            }
        }

    }

    @Override
    public void deleteByFaultIds(Collection<Long> ids) {
        // 不存在或为空直接返回
        if (ids == null || ids.size() == 0) {
            return;
        }
        LambdaQueryWrapper<TBaseFaultPhoto> lqw = Wrappers.lambdaQuery();
        lqw.in(true, TBaseFaultPhoto::getFaultId, ids);
        List<TBaseFaultPhoto> list = list(lqw);
        delete(list);
    }

    @Override
    public boolean deleteByFaultId(Long faultId) {
        if (faultId == null) {
            return true;
        }
        LambdaQueryWrapper<TBaseFaultPhoto> lqw = Wrappers.lambdaQuery();
        lqw.eq(true, TBaseFaultPhoto::getFaultId, faultId);
        return remove(lqw);
    }

    @Override
    public List<TBaseFaultPhoto> findByFaultId(Long faultId) {
        if (faultId == null) {
            return null;
        }
        LambdaQueryWrapper<TBaseFaultPhoto> lqw = Wrappers.lambdaQuery();
        lqw.eq(true, TBaseFaultPhoto::getFaultId, faultId);
        return list(lqw);
    }
}
